package com.mayuran19.portal.web.accounting.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mayuran19.portal.accounting.database.domain.ExpenseDetail;
import com.mayuran19.portal.accounting.database.domain.ExpenseHeader;
import com.mayuran19.portal.accounting.service.ExpenseDetailService;
import com.mayuran19.portal.accounting.service.ExpenseHeaderService;
import com.mayuran19.portal.common.database.domain.Product;
import com.mayuran19.portal.common.service.ProductService;
import com.mayuran19.portal.common.ui.pagination.PagedResultSet;
import com.mayuran19.portal.common.ui.pagination.Pager;
import com.mayuran19.portal.web.accounting.model.AddProductToExpenseHeaderForm;

@Controller
public class ExpensesController {
	private ProductService productService;
	private ExpenseHeaderService expenseHeaderService;
	private ExpenseDetailService expenseDetailService;

	@Autowired
	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	@Autowired
	public void setExpenseHeaderService(
			ExpenseHeaderService expenseHeaderService) {
		this.expenseHeaderService = expenseHeaderService;
	}

	@Autowired
	public void setExpenseDetailService(
			ExpenseDetailService expenseDetailService) {
		this.expenseDetailService = expenseDetailService;
	}

	@RequestMapping(value = "/accounting/expenses/add")
	public String add(Model model, Pager pager) {
		PagedResultSet pagedResultSet = this.productService.paginate(pager);
		AddProductToExpenseHeaderForm addProductToExpenseHeaderForm = new AddProductToExpenseHeaderForm();
		model.addAttribute("addProductToExpenseHeaderForm",
				addProductToExpenseHeaderForm);
		model.addAttribute("pager", pagedResultSet);
		ExpenseHeader header = this.expenseHeaderService.findById(7l,
				"expenseDetails");
		return "expense.add";
	}

	@RequestMapping(value = "/accounting/expenses/addProductToExpenseHeader", method = RequestMethod.POST)
	public @ResponseBody
	AddProductToExpenseHeaderForm addProductToExpenseHeader(
			@ModelAttribute("addProductToExpenseHeaderForm") AddProductToExpenseHeaderForm addProductToExpenseHeaderForm) {
		Product product = this.productService
				.findById(addProductToExpenseHeaderForm.getProductId());
		ExpenseHeader expenseHeader = null;
		if (addProductToExpenseHeaderForm.getExpenseId() != null
				&& addProductToExpenseHeaderForm.getExpenseId().longValue() != 0l) {
			expenseHeader = this.expenseHeaderService
					.findById(addProductToExpenseHeaderForm.getExpenseId());
		} else {
			expenseHeader = new ExpenseHeader();
		}
		ExpenseDetail expenseDetail = new ExpenseDetail();
		expenseDetail
				.setUnitPrice(addProductToExpenseHeaderForm.getUnitPrice());
		expenseDetail.setQuantity(addProductToExpenseHeaderForm.getQuantity());
		expenseDetail.setProduct(product);
		expenseHeader = this.expenseHeaderService.addProductToExpense(product,
				expenseHeader, expenseDetail);
		addProductToExpenseHeaderForm.setItemCount(expenseHeader
				.getExpenseDetails().size());
		addProductToExpenseHeaderForm.setExpenseId(expenseHeader.getId());
		addProductToExpenseHeaderForm.setExpenseTotal(expenseHeader.getTotal());
		return addProductToExpenseHeaderForm;
	}
}
